﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using Sunny.UI;

namespace CoreShop.App
{
    public partial class frmInputNumber : BaseForm
    {
        double mMaxNumber = 0;
        string sCaption = "请输入";
        public bool IsNumber = false;
        public frmInputNumber(double dMaxNumber,string _Caption)
        {
            sCaption = _Caption;
            mMaxNumber = dMaxNumber;
            InitializeComponent();
            txtNumber.TextBox.GotFocus += txtNumber_GotFocus;
        }

        private void txtNumber_GotFocus(object sender, System.EventArgs e)
        {
            numPad.SetTextBox((UIEdit)sender, UITextBox.UIEditType.Double, 2);
        }

        private void txtKey_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                double fInput;
                if ((IsNumber) && (!double.TryParse(txtNumber.Text, out fInput)))
                {
                    UIMessageBox.Show("请输入有效的数字！");
                    return;
                }
                DoOK();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void txtSendKey_Validating(object sender, CancelEventArgs e)
        {
            double fInput;
            if ((IsNumber) && (!double.TryParse(txtNumber.Text, out fInput)))
            {
                UIMessageBox.Show("请输入有效的数字！");
                e.Cancel = true;
            }
        }

        private void frmInputTxt_Shown(object sender, EventArgs e)
        {
            this.Text = sCaption;
            txtNumber.Focus();
        }

        private void numPad_OKButtonClick(object sender, string text, int intvalue, double doublevalue)
        {
            if (string.IsNullOrEmpty(txtNumber.Text))
            {
                UIMessageBox.Show("输入的内容不能为空！");
                return;
            }
            double fInput = 0;
            if ((IsNumber) && (!double.TryParse(txtNumber.Text, out fInput)))
            {
                UIMessageBox.Show("请输入有效的数字！");
                return;
            }
            if (fInput < 0)
            {
                UIMessageBox.Show("输入的数不能为负数！");
                return;
            }

            if ((fInput > mMaxNumber) && (mMaxNumber > 0))
            {
                string str = string.Format("输入的数不能大于{0}！", mMaxNumber);
                UIMessageBox.Show(str);
                return;
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void txtNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 12290)
            {
                string str = txtNumber.Text;
                int idx = txtNumber.SelectionStart;
                str = str.Insert(idx, ".");
                txtNumber.Text = str;
                txtNumber.SelectionStart = idx + 1;
                txtNumber.Focus();
                e.Handled = true;
            } else
            if (e.KeyChar == 13)
            {
                numPad_OKButtonClick(numPad, txtNumber.Text, 0, 0);
            }
        }
    }
}
